<script>
import {
  openBluetoothAdapter,startBluetoothDevicesDiscovery,onBluetoothDeviceFound,onBluetoothAdapterStateChange,
  createBLEConnection,getBLEDeviceServices,getBLEDeviceCharacteristics,
  notifyBLECharacteristicValueChange
} from "./matchblue.js"
import * as matchblue from "./matchblue.js"
import {saveFile,TimedopintFind,clearFile} from "./alyslrc.js"
	export default {
		data(){
			return{
				// blueList:[],
				// servicesList:[],
				// characteristicId:[],
				audioContext:"",
				playList:[],
				nowplay:"",//现在第几首
				playLine:[],//播放时间点集合
				playLine_index:0,//记录现在播放的时间点
				playLTime:"",//每次开始播放时间点
				timekncked:"",
				timeLine:0,
				time:0,//播放时长
				timeintervel:""
			}
		},
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			
			uni.$on("saveFile",({asserUrl,playLine,getwords})=>{
				saveFile(asserUrl,playLine,getwords)
			})
			uni.$on("openBluetoothAdapter",()=>{
				matchblue.openBluetoothAdapter()
			})
			uni.$on("startBluetoothDevicesDiscovery",()=>{
				matchblue.startBluetoothDevicesDiscovery()
			})
			uni.$on("onBluetoothDeviceFound",(bluselist)=>{
				matchblue.onBluetoothDeviceFound(bluselist)
			})
			uni.$on("onBluetoothAdapterStateChange",()=>{
				matchblue.onBluetoothAdapterStateChange()
			})
			uni.$on("createBLEConnection",({blueList,servicesList})=>{
				matchblue.createBLEConnection(blueList,servicesList)
			})
			uni.$on("getBLEDeviceCharacteristics",(blueList,servicesList)=>{
				matchblue.getBLEDeviceCharacteristics(blueList,servicesList)
			})
			uni.$on("getBLEDeviceServices",({blueList,servicesList})=>{
				matchblue.getBLEDeviceServices(blueList,servicesList)
			})
			uni.$on("notifyBLECharacteristicValueChange",()=>{
				matchblue. notifyBLECharacteristicValueChange()
			})
			uni.$on("createInnerAudioContext",()=>{
				this.audioContext = uni.createInnerAudioContext()

			})
			uni.$on("InnerAudioContext_init",({src,autoplay,duration})=>{
				this.audioContext.autoplay = autoplay;
				this.audioContext.src = src;
				// this.audioContext.currentTime = 93;
				
				this.audioContext.onPlay(() => {
				  console.log('开始播放1',this.audioContext.duration);
				});
				this.audioContext.onCanplay(() => {
				const { currentTime , duration ,buffered} = this.audioContext;  
				  console.log('开始播放2',this.audioContext,duration);
				//   setTimeout(()=>{
				//   console.log('开始播放2',this.audioContext.duration,this.audioContext);
				//   },100)
				//   duration = this.audioContext.duration
				});

			})
			uni.$on("createInnerAudioContext_destroy",()=>{
				if(this.audioContext)this.audioContext.destroy()
				// this.timeLine = 0
				if(this.timekncked)clearInterval(this.timekncked)
					// this.audioContext.desdestroy()
			})
			uni.$on("innerAudioContext_seek",(number)=>{
				if(this.audioContext)this.audioContext.seek(number)
					// this.audioContext.desdestroy()
			})
			uni.$on("innerAudioContext_onCanplay",(callback)=>{
				if(this.audioContext)this.audioContext.onCanplay(callback)
					// this.audioContext.desdestroy()
			})
			// 
			uni.$on("Getlist",(list)=>{
				this.playList = list
				// if(!list){
				// 	this.reqtest()
				// }
			})
			uni.$on("bluePlay",(id,getwords)=>{
				if(id) this.nowplay = id
				this.initTime()
				console.log(this.playList[this.nowplay]);
				console.log("timelineeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",this.timeLine,this.nowplay);
				if(!this.nowplay){
					this.nowplay = 0
					this.playaudio(this.playList[0])
					uni.$emit("saveFile",{asserUrl:this.playList[0].asserUrl,playLine:this.playLine,getwords})
				}
				if(this.nowplay&&this.timeLine==0){
					this.playaudio(this.playList[this.nowplay])
					uni.$emit("saveFile",{asserUrl:this.playList[this.nowplay].asserUrl,playLine:this.playLine,getwords})
				}
				if(this.timeLine>=0){
					setTimeout(()=>{
					uni.$emit("innerAudioContext_seek",this.timeLine)
					},100)
				  uni.$emit("saveFile",{asserUrl:this.playList[this.nowplay].asserUrl,playLine:this.playLine,getwords})

				}
			})
			uni.$on("bluePlayNext",()=>{
				// 增加用户时长
			this.Addtime()
			this.setInterval = setInterval(() => {
				this.time++
			}, 1000);
			// 
			this.timeLine = 0
			this.initTime()	
			if(!this.nowplay){
				this.nowplay = 0
				this.playaudio(this.playList[0])
			}
			if(this.nowplay){
				// if(this.nowplay>this.playList.length)
				this.nowplay++
				if(this.nowplay>this.playList.length)this.nowplay = 0
				this.playaudio(this.playList[this.nowplay])
			}
			})
			uni.$on("bluePlayPre",()=>{
			// 增加用户时长
			this.Addtime()
			this.setInterval = setInterval(() => {
				this.time++
			}, 1000);
			// 
			this.timeLine = 0	
			this.initTime()	
			if(!this.nowplay){
				this.nowplay = 0
				this.playaudio(this.playList[0])
			}
			if(this.nowplay){
				this.nowplay--
				if(this.nowplay<=0){
					this.nowplay = this.playList.length
					this.playaudio(this.playList[this.nowplay])
				}
				if(this.nowplay>=0){
					this.playaudio(this.playList[this.nowplay])
				}
			}
			})
			uni.$on("changewords", async ({where})=>{
				if(where=="pre"){
					// this.playLine_index--
					console.log("this.timeLine",this.timeLine);
					let a =	await TimedopintFind(this.playLine,this.timeLine,this.playLine_index)
					console.log("a",a);
					// this.timeLine = this.playLine[a]
					this.playLine_index = a
					
					this.playLine_index--
					this.playLine_index--

					// this.timeLine = this.playLine[this.playLine_index]
					// console.log("this.playLine_index",this.playLine_index);
					// console.log("this.playLine_index",a);
					// console.log("this.playLine",this.playLine);
					// console.log("this.timeLine",this.timeLine);
					if(this.playLine_index<0)this.playLine_index = 0
					this.timeLine = this.playLine[this.playLine_index]
					uni.$emit("innerAudioContext_seek",this.playLine[this.playLine_index])
				}
				if(where=="next"){
					let a =	await TimedopintFind(this.playLine,this.timeLine,this.playLine_index)
					console.log("a",a);
					// this.timeLine = this.playLine[a]
					this.playLine_index = a
					// this.playLine_index++

					// TimedopintFind(this.playLine,this.audioContext.currentTime,this.playLine_index)
					if(this.playLine_index>this.playLine.length)this.playLine_index = this.playLine.length
					this.timeLine = this.playLine[this.playLine_index]
					uni.$emit("innerAudioContext_seek",this.playLine[this.playLine_index])
				}
			})
			uni.$on("blueChangevolume",(change)=>{
				if(this.audioContext){
					console.log("this.audioContext.volume",this.audioContext.volume,change);
					if(change == "up"){
						this.audioContext.volume += 0.1
						if(this.audioContext.volume>=1)this.audioContext.volume=1
					}
					if(change == "down"){
						this.audioContext.volume -= 0.1
						if(this.audioContext.volume<=0)this.audioContext.volume=0
					}
					this.audioContext.volume = Number(this.audioContext.volume.toFixed(1)) 
					//  this.audioContext.volume = 0.5;
				}
			})
			// 蓝牙暂停
			// uni.$on("blue_stop",()=>{
				
			// })
			// 清除之前缓存
			clearFile()
			// f
				// uni.$on("Listenersadd",(id,func)=>{
				// 	console.log("this.$refs[id]",func);
				// 	// this.$refs[0][id].heardNumAdd()
				// 	func()
				// })
			// 
			//  uni.setStorageSync(String(1),"1233")
			//  wx.setStorageSync(String(1),true)
			// 
			
		},
		methods:{
			 async playaudio(item){
                   console.log("this.playList",this.playList);
                   await uni.$emit("createInnerAudioContext_destroy")
                   await uni.$emit("createInnerAudioContext")
                   await uni.$emit("InnerAudioContext_init",{src:item.audioUrl,autoplay:true})
                   console.log("in");
                },
			 async	reqtest(){
			 	// 
			uni.request({
			    url: 'http://192.168.31.18:8889/app/home/article', 
				method:"GET",
			    data: {
			    },
			    header: {
			    },
			    success: (res) => {
			        console.log(res.data);
					this.showList = res.data.rows;
					this.playList.contains(res.data.rows)
			    }
			});
				// 
	
			// 
			},
		async initTime(){
			if(this.timekncked){clearInterval(this.timekncked)}
			 this.timekncked = await setInterval(()=>{
					this.timeLine += 0.1
				},100)
		},
		async Addtime(){
			clearInterval(this.timeintervel)
			let api = `${this.$cache.get("UserName")}/?time=${this.time}`
			console.log(`this.$cache.get("UserName")`,this.$cache.get("UserName"));
			if(this.$cache.get("UserName")){
			await	this.$myRequest({
						url:'/app/home/add_user_time/'+api,
						method:'get',
						data:{
						}
					}).then(res=>{
							console.log("返回参数",res);
							console.log("点赞成功")
							this.time = 0
						}
						).catch(err =>{
							console.log(err)
						})			
			}
			if(this.time>60){
				 uni.request({
                    			  url: 'http://139.9.151.197:8889/app/home/article/'+ this.playLine(this.nowplay).id,
                    			  complete: (res) => {},
                    			  data: {},
                    			  fail: (res) => {},
                    			  method: "GET",
                    			  success: (result) => {
                    			      console.log("result",result);
                    			  },
                    			})
			}
		}		
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
</style>
